package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.PanghuCommunity;
import com.hualan.dao.CommunityDao;
import com.hualan.dao.CommunityDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/addcommunity")
public class CommunityAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CommunityDao communityDao = new CommunityDaoImpl();

        String communityName = req.getParameter("communityName");
        String communityAddress = req.getParameter("communityAddress");
        String communityProvince = req.getParameter("communityProvince");
        String communityCity = req.getParameter("communityCity");
        String communityDistrict = req.getParameter("communityDistrict");
        String communityLatitude = req.getParameter("communityLatitude");
        String communityLongitude = req.getParameter("communityLongitude");
        String communityCoordinates = req.getParameter("communityCoordinates");
        String communityAreaCode = req.getParameter("communityAreaCode");
        System.out.println(communityName);
        System.out.println(communityAddress);
        System.out.println(communityProvince);
        System.out.println(communityCity);
        System.out.println(communityDistrict);
        System.out.println(communityLatitude);
        System.out.println(communityLongitude);
        System.out.println(communityCoordinates);
        System.out.println(communityAreaCode);

        PanghuCommunity community = new PanghuCommunity(communityName,communityAddress,communityProvince,communityCity,communityDistrict,communityLatitude,communityLongitude,communityCoordinates,communityAreaCode);
        int num = communityDao.addCommunity(community);
        Map<String, Object> map = new HashMap<>();
        if (num > 0){
            map.put("code",200);
        }else {
            map.put("code",300);
        }

        resp.getWriter().write(JSON.toJSONString(map));
    }
    }

